package com.april.coupon.controller;

import com.april.common.utils.PageUtils;
import com.april.common.utils.Result;
import com.april.coupon.entity.CouponEntity;
import com.april.coupon.model.AprilEntity;
import com.april.coupon.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 优惠券信息
 *
 * @author april
 * @email 2673888518@qq.com
 * @date 2023-07-15 16:14:22
 */
@RefreshScope
@RestController
@RequestMapping("coupon/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;
    @Value("${coupon.user.name}")
    private String name;
    @Value("${coupon.user.age}")
    private String age;

    @Autowired
    private AprilEntity april;

    @RequestMapping("/test")
    public Result test(){
        // april.setName(name);
        // april.setAge(age);
        // return Result.ok().put("name",april.getName()).put("age",april.getAge());
        return Result.ok().put("name",april.getName()).put("age",april.getAge());
        // return Result.ok().put("name",name).put("age",age);
    }

    @RequestMapping("/member/list")
    public Result memberCoupons() {
        CouponEntity couponEntity = new CouponEntity();
        couponEntity.setCouponName("满一百减八十");
        return Result.ok().put("coupons",Arrays.asList(couponEntity));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        PageUtils page = couponService.queryPage(params);

        return Result.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id) {
        CouponEntity coupon = couponService.getById(id);

        return Result.ok().put("coupon", coupon);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody CouponEntity coupon) {
        couponService.save(coupon);

        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody CouponEntity coupon) {
        couponService.updateById(coupon);

        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Long[] ids) {
        couponService.removeByIds(Arrays.asList(ids));

        return Result.ok();
    }

}
